const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    hasAutoPrintReceipt: false,
    realName: "",
    mobile: "",
    cardCode: "",
    showPickerCrop: false,
    farmerCrops: [],
    cropList: [],
    result: [],
    newList: [],
    columns: ["精品", "次品", "统货"],
    activeVarietyId: null
  },

  // 品质选择
  onSelectQuality(e) {
    let activeVarietyId = e.currentTarget.dataset.id;
    this.setData({
      activeVarietyId,
      showPicker: true
    });
  },

  onPickerChange(event) {
    const { value, index } = event.detail;
    const { activeVarietyId, newList } = this.data;
    let newIndex = newList.findIndex((item) => item.varietyId === activeVarietyId);
    newList[newIndex]["qualityType"] = index + 1;
    newList[newIndex]["qualityTypeName"] = value;
    this.setData({
      newList,
      activeVarietyId: null,
      showPicker: false
    });
  },

  onCancel() {
    this.setData({
      activeVarietyId: null,
      showPicker: false
    });
  },

  onSwitchChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      hasAutoPrintReceipt: detail
    });
  },
  // 删除卡
  onDelete(e) {
    this.setData({
      cardCode: ""
    });
    // let index = e.currentTarget.dataset.index;
    // let farmerCrops = this.data.farmerCrops;
    // farmerCrops.splice(index, 1);
    // this.setData({
    //   farmerCrops
    // });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      id
    });
    this.getCrops().then((res) => {
      this.getData(id);
    });
  },

  // 获取详情数据
  getData(id) {
    wx.showLoading({
      title: "查询中..."
    });
    wx.$apis.getUserInfo(id).then((res) => {
      if (res.data) {
        const { hasAutoPrintReceipt, realName, mobile, farmerCrops, cardCode } = res.data;
        this.setData({
          newList: farmerCrops,
          hasAutoPrintReceipt,
          realName,
          mobile,
          farmerCrops,
          cardCode
        });
      }
    });
  },

  // 确认修改
  onConfirm() {
    const id = this.data.id * 1;
    const { hasAutoPrintReceipt, realName, mobile, farmerCrops, newList, cardCode } = this.data;
    let params = {
      id,
      hasAutoPrintReceipt,
      realName,
      mobile,
      cardCode,
      farmerCrops: newList.map((item) => {
        return {
          cropId: item.cropId,
          cropName: item.cropName,
          varietyId: item.varietyId,
          varietyName: item.varietyName,
          qualityType: item.qualityType
        };
      }),
      marketRoleIds: [2]
    };

    wx.showLoading({
      title: "提交中..."
    });
    wx.$apis.updateMarketUser(params).then((res) => {
      wx.navigateBack();
    });
  },

  // 输入框值检测
  onChange(e) {
    console.log(e);
  },

  noop() {},

  // 农产品选择弹框
  pickerCrops() {
    this.setData({
      result: this.data.newList.map((item) => {
        return item.varietyName;
      }),
      showPickerCrop: true
    });
  },

  // 获取作物信息
  getCrops() {
    return new Promise((reslove, reject) => {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis
        .getCropCategoriesByCompanyId(app.globalData.userInfo.companyData.companyId)
        .then((res) => {
          let categoryList = res.data.filter((item) => item.varietyId);
          this.setData({
            categoryList: categoryList.map((item) => {
              return {
                ...item,
                qualityType: 1, // 1-精品 2-次品 3-统货
                qualityTypeName: "精品",
                value: item.peelingType === 1 ? "一次除皮" : "二次除皮"
              };
            })
          });
          reslove(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },

  onCheckboxChange(event) {
    this.setData({
      result: event.detail
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  cancelcrops() {
    this.setData({
      showPickerCrop: false
    });
  },
  // 确认农产品
  surecrops() {
    let newList = [];
    this.data.result.map((kk) => {
      let obj = {};
      obj = this.data.categoryList.find((item) => {
        return item.varietyName == kk;
      });
      newList.push(obj);
    });
    this.setData({
      showPickerCrop: false,
      newList
    });
  }
});
